# $ANTLR 3.1.1 simple_pyexpression.g 2009-01-14 21:19:20

import sys
from antlr3 import *
from antlr3.compat import set, frozenset


# for convenience in actions
HIDDEN = BaseRecognizer.HIDDEN

# token types
WS=6
NEWLINE=4
T__12=12
T__11=11
T__10=10
INT=5
EOF=-1
T__9=9
T__8=8
T__7=7

# token names
tokenNames = [
    "<invalid>", "<EOR>", "<DOWN>", "<UP>", 
    "NEWLINE", "INT", "WS", "'+'", "'-'", "'*'", "'/'", "'('", "')'"
]




class simple_pyexpressionParser(Parser):
    grammarFileName = "simple_pyexpression.g"
    antlr_version = version_str_to_tuple("3.1.1")
    antlr_version_str = "3.1.1"
    tokenNames = tokenNames

    def __init__(self, input, state=None):
        if state is None:
            state = RecognizerSharedState()

        Parser.__init__(self, input, state)







                


        



    # $ANTLR start "prog"
    # simple_pyexpression.g:6:1: prog : ( stat )+ ;
    def prog(self, ):

        try:
            try:
                # simple_pyexpression.g:6:5: ( ( stat )+ )
                # simple_pyexpression.g:6:9: ( stat )+
                pass 
                # simple_pyexpression.g:6:9: ( stat )+
                cnt1 = 0
                while True: #loop1
                    alt1 = 2
                    LA1_0 = self.input.LA(1)

                    if ((NEWLINE <= LA1_0 <= INT) or LA1_0 == 11) :
                        alt1 = 1


                    if alt1 == 1:
                        # simple_pyexpression.g:6:9: stat
                        pass 
                        self._state.following.append(self.FOLLOW_stat_in_prog24)
                        self.stat()

                        self._state.following.pop()


                    else:
                        if cnt1 >= 1:
                            break #loop1

                        eee = EarlyExitException(1, self.input)
                        raise eee

                    cnt1 += 1






            except RecognitionException, re:
                self.reportError(re)
                self.recover(self.input, re)
        finally:

            pass

        return 

    # $ANTLR end "prog"


    # $ANTLR start "stat"
    # simple_pyexpression.g:8:1: stat : ( expr NEWLINE | NEWLINE );
    def stat(self, ):

        try:
            try:
                # simple_pyexpression.g:8:5: ( expr NEWLINE | NEWLINE )
                alt2 = 2
                LA2_0 = self.input.LA(1)

                if (LA2_0 == INT or LA2_0 == 11) :
                    alt2 = 1
                elif (LA2_0 == NEWLINE) :
                    alt2 = 2
                else:
                    nvae = NoViableAltException("", 2, 0, self.input)

                    raise nvae

                if alt2 == 1:
                    # simple_pyexpression.g:8:9: expr NEWLINE
                    pass 
                    self._state.following.append(self.FOLLOW_expr_in_stat51)
                    self.expr()

                    self._state.following.pop()
                    self.match(self.input, NEWLINE, self.FOLLOW_NEWLINE_in_stat53)


                elif alt2 == 2:
                    # simple_pyexpression.g:9:9: NEWLINE
                    pass 
                    self.match(self.input, NEWLINE, self.FOLLOW_NEWLINE_in_stat63)



            except RecognitionException, re:
                self.reportError(re)
                self.recover(self.input, re)
        finally:

            pass

        return 

    # $ANTLR end "stat"


    # $ANTLR start "expr"
    # simple_pyexpression.g:12:1: expr : multExpr ( '+' multExpr | '-' multExpr )* ;
    def expr(self, ):

        try:
            try:
                # simple_pyexpression.g:12:6: ( multExpr ( '+' multExpr | '-' multExpr )* )
                # simple_pyexpression.g:12:10: multExpr ( '+' multExpr | '-' multExpr )*
                pass 
                self._state.following.append(self.FOLLOW_multExpr_in_expr78)
                self.multExpr()

                self._state.following.pop()
                # simple_pyexpression.g:12:19: ( '+' multExpr | '-' multExpr )*
                while True: #loop3
                    alt3 = 3
                    LA3_0 = self.input.LA(1)

                    if (LA3_0 == 7) :
                        alt3 = 1
                    elif (LA3_0 == 8) :
                        alt3 = 2


                    if alt3 == 1:
                        # simple_pyexpression.g:12:21: '+' multExpr
                        pass 
                        self.match(self.input, 7, self.FOLLOW_7_in_expr82)
                        self._state.following.append(self.FOLLOW_multExpr_in_expr84)
                        self.multExpr()

                        self._state.following.pop()


                    elif alt3 == 2:
                        # simple_pyexpression.g:12:36: '-' multExpr
                        pass 
                        self.match(self.input, 8, self.FOLLOW_8_in_expr88)
                        self._state.following.append(self.FOLLOW_multExpr_in_expr90)
                        self.multExpr()

                        self._state.following.pop()


                    else:
                        break #loop3






            except RecognitionException, re:
                self.reportError(re)
                self.recover(self.input, re)
        finally:

            pass

        return 

    # $ANTLR end "expr"


    # $ANTLR start "multExpr"
    # simple_pyexpression.g:14:1: multExpr : atom ( '*' atom | '/' atom )* ;
    def multExpr(self, ):

        try:
            try:
                # simple_pyexpression.g:14:10: ( atom ( '*' atom | '/' atom )* )
                # simple_pyexpression.g:14:13: atom ( '*' atom | '/' atom )*
                pass 
                self._state.following.append(self.FOLLOW_atom_in_multExpr103)
                self.atom()

                self._state.following.pop()
                # simple_pyexpression.g:14:18: ( '*' atom | '/' atom )*
                while True: #loop4
                    alt4 = 3
                    LA4_0 = self.input.LA(1)

                    if (LA4_0 == 9) :
                        alt4 = 1
                    elif (LA4_0 == 10) :
                        alt4 = 2


                    if alt4 == 1:
                        # simple_pyexpression.g:14:19: '*' atom
                        pass 
                        self.match(self.input, 9, self.FOLLOW_9_in_multExpr106)
                        self._state.following.append(self.FOLLOW_atom_in_multExpr108)
                        self.atom()

                        self._state.following.pop()


                    elif alt4 == 2:
                        # simple_pyexpression.g:14:30: '/' atom
                        pass 
                        self.match(self.input, 10, self.FOLLOW_10_in_multExpr112)
                        self._state.following.append(self.FOLLOW_atom_in_multExpr114)
                        self.atom()

                        self._state.following.pop()


                    else:
                        break #loop4






            except RecognitionException, re:
                self.reportError(re)
                self.recover(self.input, re)
        finally:

            pass

        return 

    # $ANTLR end "multExpr"


    # $ANTLR start "atom"
    # simple_pyexpression.g:17:1: atom : ( INT | '(' expr ')' );
    def atom(self, ):

        try:
            try:
                # simple_pyexpression.g:17:6: ( INT | '(' expr ')' )
                alt5 = 2
                LA5_0 = self.input.LA(1)

                if (LA5_0 == INT) :
                    alt5 = 1
                elif (LA5_0 == 11) :
                    alt5 = 2
                else:
                    nvae = NoViableAltException("", 5, 0, self.input)

                    raise nvae

                if alt5 == 1:
                    # simple_pyexpression.g:17:9: INT
                    pass 
                    self.match(self.input, INT, self.FOLLOW_INT_in_atom131)


                elif alt5 == 2:
                    # simple_pyexpression.g:18:9: '(' expr ')'
                    pass 
                    self.match(self.input, 11, self.FOLLOW_11_in_atom141)
                    self._state.following.append(self.FOLLOW_expr_in_atom143)
                    self.expr()

                    self._state.following.pop()
                    self.match(self.input, 12, self.FOLLOW_12_in_atom145)



            except RecognitionException, re:
                self.reportError(re)
                self.recover(self.input, re)
        finally:

            pass

        return 

    # $ANTLR end "atom"


    # Delegated rules


 

    FOLLOW_stat_in_prog24 = frozenset([1, 4, 5, 11])
    FOLLOW_expr_in_stat51 = frozenset([4])
    FOLLOW_NEWLINE_in_stat53 = frozenset([1])
    FOLLOW_NEWLINE_in_stat63 = frozenset([1])
    FOLLOW_multExpr_in_expr78 = frozenset([1, 7, 8])
    FOLLOW_7_in_expr82 = frozenset([5, 11])
    FOLLOW_multExpr_in_expr84 = frozenset([1, 7, 8])
    FOLLOW_8_in_expr88 = frozenset([5, 11])
    FOLLOW_multExpr_in_expr90 = frozenset([1, 7, 8])
    FOLLOW_atom_in_multExpr103 = frozenset([1, 9, 10])
    FOLLOW_9_in_multExpr106 = frozenset([5, 11])
    FOLLOW_atom_in_multExpr108 = frozenset([1, 9, 10])
    FOLLOW_10_in_multExpr112 = frozenset([5, 11])
    FOLLOW_atom_in_multExpr114 = frozenset([1, 9, 10])
    FOLLOW_INT_in_atom131 = frozenset([1])
    FOLLOW_11_in_atom141 = frozenset([5, 11])
    FOLLOW_expr_in_atom143 = frozenset([12])
    FOLLOW_12_in_atom145 = frozenset([1])



def main(argv, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr):
    from antlr3.main import ParserMain
    main = ParserMain("simple_pyexpressionLexer", simple_pyexpressionParser)
    main.stdin = stdin
    main.stdout = stdout
    main.stderr = stderr
    main.execute(argv)


if __name__ == '__main__':
    main(sys.argv)
